package site.shihuan.helpdesk.im.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import site.shihuan.helpdesk.im.command.BaseCommand;
import site.shihuan.helpdesk.im.command.impl.ChatCommand;
import site.shihuan.helpdesk.im.command.impl.RtcCommand;
import site.shihuan.helpdesk.im.message.BaseMessage;
import site.shihuan.helpdesk.im.exception.CommandException;

import javax.websocket.Session;
import java.io.IOException;

/**
 * 消息统一处理
 *
 * @author 周世焕
 * @Date 2021/2/2 21:08
 */
@Component
public class CommandHandler {

    @Autowired
    private ChatCommand chatCommand;

    @Autowired
    private RtcCommand rtcCommand;

    public void handleCommand(Session session, BaseMessage baseMessage) throws CommandException, IOException {
        BaseCommand command = getCommand(baseMessage);
        if(command == null) {
            return;
        }
        command.execute(session, baseMessage);
    }

    private BaseCommand getCommand(BaseMessage baseMessage) {
        switch (baseMessage.getMessageType()) {
            case BaseMessage.TYPE_CHAT:
                return chatCommand;
            case BaseMessage.TYPE_RTC_CONNECT:
                return rtcCommand;
            default:
                break;
        }
        return null;
    }
}
